/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.exp.security.vo;

import cn.hutool.http.HttpStatus;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 统一API响应结果封装
 *
 * @author Chill
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
	private int code;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 数据对象
     */
    private T data;

    /**
     * 返回消息
     */
    private String msg;

	public R(int code, boolean success, T data, String msg) {
		this.code = code;
		this.success = success;
		this.data = data;
		this.msg = msg;
	}

	/**
	 * 返回R
	 *
	 * @param data 数据
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(T data) {
		return data(data, null);
	}

	public static <T> R<T> data(T data, String msg) {
		return new R<>(HttpStatus.HTTP_OK, Boolean.TRUE, data, msg);
	}

	/**
	 * 返回R
	 *
	 * @param msg 消息
	 * @param <T> T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(String msg) {
		return fail(HttpStatus.HTTP_BAD_REQUEST, msg);
	}

	public static <T> R<T> fail(int code, String msg) {
		return new R<>(code, Boolean.FALSE, null, msg);
	}



}
